package com.loong.shop.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.common.rabbitmq.dto.base.RepositoryEventDto;
import com.loong.common.utils.TransformUtils;
import com.loong.shop.common.backend.param.GoodsCategoryReportParam;
import com.loong.shop.model.GoodsCategory;
import com.loong.shop.mq.GoodsCategoryReport;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author: 刘群
 * @Date: 2020/10/27 7:47
 */
@Repository
public class GoodsCategoryBusiness extends AbstractBusiness<GoodsCategory> {
    @Resource
    private GoodsCategoryReport goodsCategoryReport;

    /**
     * 根据类别名称查询商品类别
     *
     * @param categoryName 类别名称
     * @return 商品类别集合
     */
    public List<GoodsCategory> findCategoryByName(String categoryName) {
        Condition condition = new Condition(GoodsCategory.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("categoryName", categoryName);
        return findByCondition(condition);
    }

    /**
     * 根据parentId删除商品类别
     *
     * @param parentId 父类别id
     */
    public void deleteCategoryNameByParentId(Long parentId) {
        Condition condition = new Condition(GoodsCategory.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("parentId", parentId);
        deleteByCondition(condition);
    }

    /**
     * 根据店铺id搜索商品分类
     * @param shopId 店铺id
     * @return 商品分类集合
     */
    public List<GoodsCategory> findCategoryByShopId(Long shopId){
        Condition condition = new Condition(GoodsCategory.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("shopId",shopId);
        return findByCondition(condition);
    }

    @Override
    public void save(GoodsCategory model) {
        super.save(model);
        GoodsCategoryReportParam param = TransformUtils.transform(model, GoodsCategoryReportParam.class);
        RepositoryEventDto<GoodsCategoryReportParam> goodsCategoryRepositoryEventDto = RepositoryEventDto.buildWithCreate(param);
        goodsCategoryReport.report(goodsCategoryRepositoryEventDto);
    }

    @Override
    public void updateByPrimaryKeySelective(GoodsCategory model) {
        super.updateByPrimaryKeySelective(model);
        GoodsCategoryReportParam param = TransformUtils.transform(model, GoodsCategoryReportParam.class);
        RepositoryEventDto<GoodsCategoryReportParam> goodsCategoryRepositoryEventDto = RepositoryEventDto.buildWithUpdate(param);
        goodsCategoryReport.report(goodsCategoryRepositoryEventDto);
    }


    @Override
    public void deleteByIds(String ids) {
        super.deleteByIds(ids);
        GoodsCategoryReportParam param = GoodsCategoryReportParam.builder().ids(ids).build();
        RepositoryEventDto<GoodsCategoryReportParam> goodsCategoryRepositoryEventDto = RepositoryEventDto.buildWithDelete(param);
        goodsCategoryReport.report(goodsCategoryRepositoryEventDto);

    }
}
